import {
	serverRegistByPhone,
	getServerUserInfo,
	webUserInit,
	loginByNo,
	loginByOpenId,
	getUserData,
	loginByAdminAccount
} from '../../api/user.js'
import {
	setUserInfo,
	getUserInfo
} from '../../utils/tools.js'

export default {
	state: {
		userInfo: getUserInfo()
	},
	mutations: {
		setUserState(state, payload) {
			setUserInfo(payload)
			state.userInfo = payload
		},
		setAccountLoginUserState(state, payload) {
			state.userInfo = payload
		}
	},
	getters: {

	},
	actions: {
		serverWXLogin({
			commit
		}, payload) {
			return new Promise((resolve, reject) => {
				setTimeout(() => {
					let res = {
						openId: '888'
					}
					commit('setUserState', res)
					resolve(res)
				}, 1000)
			})
		},
		serverLoginByPhone() {
			return new Promise((resolve, reject) => {
				setTimeout(() => {
					resolve()
				}, 1000)
			})
		},
		serverRegistByPhone({}, payload) {
			return new Promise((resolve, reject) => {
				serverRegistByPhone(payload).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		getServerUserInfo({}, payload) {
			return new Promise((resolve, reject) => {
				getServerUserInfo(payload).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		webUserInit({}, payload) {
			return new Promise((resolve, reject) => {
				webUserInit(payload).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		loginByNo({}, payload) {
			return new Promise((resolve, reject) => {
				loginByNo(payload).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		loginByOpenId({}, payload) {
			return new Promise((resolve, reject) => {
				loginByOpenId(payload).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		getUserData({}, payload) {
			return new Promise((resolve, reject) => {
				getUserData(payload).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		loginByAdminAccount({}, payload) {
			return new Promise((resolve, reject) => {
				loginByAdminAccount(payload).then(res => {
					if (res && res.code == '000000') {
						resolve(res.data)
					} else {
						if (typeof res == String && res.indexOf("err@") == 0) {
							reject(res.split('err@')[0])
						} else {
							reject('登录失败')
						}

					}
				}).catch(err => {
					reject('登录失败')
				})
			})
		}
	},
	namespaced: true
}
